package top.maof.book.params;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


@Data
@ApiModel("查询")
public class Param implements Serializable {
    @ApiModelProperty("升序")
    protected List<String> asc;

    @ApiModelProperty("降序")
    protected List<String> desc;

    protected Long page;

    protected Long pageSize;


    public <T> IPage<T> iPage() {
        IPage<T> page = new Page<>(this.page, this.pageSize);
        List<OrderItem> orderItems = orderItems();
        if (orderItems != null && orderItems.size() > 0) {
            ((Page<T>) page).setOrders(orderItems);
        }
        return page;
    }

    private List<OrderItem> orderItems() {
        List<OrderItem> list = new ArrayList<>();
        if (asc != null) {
            for (String s : asc) {
                OrderItem orderItem = new OrderItem();
                orderItem.setAsc(true);
                orderItem.setColumn(s);
                list.add(orderItem);
            }
        }
        if (desc != null) {
            for (String s : desc) {
                OrderItem orderItem = new OrderItem();
                orderItem.setAsc(false);
                orderItem.setColumn(s);
                list.add(orderItem);
            }
        }
        return list;
    }
}
